package com.bw.mallshop.mvvm.viewmodel;

import com.bw.mallshop.MainActivity;
import com.bw.mallshop.mvvm.model.SplashModel;
import com.bw.mallshop.mvvm.model.entity.TokenEntity;
import com.bw.mallshop.timmer.TimeHandler;
import com.bw.mallshop.timmer.TimeThread;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.field.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.mvvm_common.utils.SPUtils;

import java.util.HashMap;

import androidx.databinding.ObservableField;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SplashViewModel extends BaseViewModel<SplashModel> {

    public ObservableField<String>timeObserable=new ObservableField<>("3S");
    private TimeHandler handler;

    @Override
    public void onCreate() {
        super.onCreate();
        //页面加载完成后
        //请求token
        requestToken();
        handler = new TimeHandler(this);
    }

    private void requestToken(){
        HashMap<String,Object>map = new HashMap<>();
        map.put("imie",""+System.currentTimeMillis());
        m.requestToken(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void result(BaseEntity entity) {
        //判断返回值状态是否请求成功
        if (entity.getMessage().equals("imie为空")){
            showMsg(entity.getMessage());
            return;
        }
        if (entity.getMessage().equals("Sign为空")){
            showMsg(entity.getMessage());
            return;
        }
        TokenEntity tokenEntity = (TokenEntity) entity;
        //成功token保存本地
        SPUtils.writeToken(tokenEntity.getValues().token);
        //启动定时线程定时3S
        new TimeThread(handler).start();
    }

    public void skipTimmer(){
        HashMap<String,Object>map = new HashMap<>();
        map.put(UIField.PAGECLAZZ, MainActivity.class);
        startPage(map);
    }

}
